from flask import Blueprint,request,jsonify
from stationpilot.utils import sec,resp

import hashlib

api = Blueprint("user", __name__, url_prefix="/user")

@api.route("/login", methods=["POST"])
def login():
    # 确保 Content-Type 是 application/json
    if not request.is_json:
        return resp.resp_builder(None, msg="Request must be JSON", code=400)

    data = request.get_json()

    username = data.get("username")
    password = data.get("password")

    if username != 'admin' and password != 'admin':
        return resp.resp_builder(None,msg="Unknown Error",code=400)

    # 简单验证一下
    if username == 'admin' and password == 'admin':
        return resp.resp_builder({"token": sec.sha256_hash(username)},msg="success")
    
